package com.cskaoyan.aspect;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.springframework.stereotype.Component;

import java.util.Arrays;

/**
 * @author stone
 * @date 2022/03/22 16:01
 */
@Component
public class CustomAspect {

    public void before(JoinPoint joinPoint) {
        Object proxy = joinPoint.getThis(); //代理对象
        Object target = joinPoint.getTarget();//委托对象

        Object[] args = joinPoint.getArgs();//传入的参数
        System.out.println(Arrays.asList(args));

        Signature signature = joinPoint.getSignature();
        String name = signature.getName();
        System.out.println(name);

    }

    public Object around(ProceedingJoinPoint joinPoint) throws Throwable {

        //jointPoint.proceed();         → 参数的保持不变
        //jointPoint.proceed(Object[]); → 以传入的Object[]作为参数执行委托类方法

        // hello songge , byebye songge
        //参数：songge → 参数：songge , byebye songge
        Object[] args = joinPoint.getArgs();
        if (joinPoint.getSignature().getName().equals("sayHello")) {
            args[0] = args[0] + " , byebye songge";
            Object proceed = joinPoint.proceed(args);
            return proceed;
        }
        Object proceed = joinPoint.proceed();
        return proceed;
    }
}
